<template>
  <div class="app-container">
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="字典名称">
        <template slot-scope="{row}">
          <span>{{ row.dict_name }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="字典编号">
        <template slot-scope="{row}">
          <span>{{ row.dict_code }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" label="描述">
        <template slot-scope="{row}">
          <svg-icon v-for="n in + row.importance" :key="n" icon-class="star" class="meta-item__icon"/>
        </template>
      </el-table-column>

      <el-table-column width="140px" class-name="status-col" label="创建时间">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="创建者">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.title" class="edit-input" size="small"/>
            <el-button
              class="cancel-btn"
              size="small"
              icon="el-icon-refresh"
              type="warning"
              @click="cancelEdit(row)"
            >
              cancel
            </el-button>
          </template>
          <span v-else>{{ row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="{row}">
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmEdit(row)"
          >
            Ok
          </el-button>
          <el-button
            v-else
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="row.edit=!row.edit"
          >
            Edit
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="query.pagination.total>0" :total="query.pagination.total" :page.sync="query.pagination.page"
                :limit.sync="query.listQuery.limit"
                @pagination="getList"
    />
  </div>
</template>

<script>
import { dictPagination, fetchList } from '@/api/article'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'

export default {
  name: 'InlineEditTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      query: {
        listQuery: {
          dict_name: null,
          dict_code: null,
          description: null,
          id: null
          // sort: '+id'
        },
        pagination: {
          total: 0,
          current: 0,
          page: 0,
          page_info: {
            since_id: 0,
            max_id: 0
          },
          limit: 10
        }
      },
      importanceOptions: [1, 2, 3],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        dict_name: '',
        dict_code: '',
        urgent: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改字典',
        create: '创建字典'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: '请填写项目类型', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: '请填写时间', trigger: 'change' }],
        title: [{ required: true, message: '请填写标题', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      dictPagination(this.query).then(response => {
        this.list = response.data.dic
        this.total = response.pagination.total
        this.query.pagination = response.pagination
        this.listLoading = false
      })
    },
    handleFetchPv(pv) {
      fetchPv(pv).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    },
    handleFilter() {
      this.query.pagination.page = 1
      this.getList()
    },
    cancelEdit(row) {
      row.title = row.originalTitle
      row.edit = false
      this.$message({
        message: 'The title has been restored to the original value',
        type: 'warning'
      })
    },
    confirmEdit(row) {
      row.edit = false
      row.originalTitle = row.title
      this.$message({
        message: 'The title has been edited',
        type: 'success'
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
